<?php

/**
 *    支付宝支付方式插件
 *
 *    @author    Garbin
 *    @usage    none
 */
class EpayalipaywapPayment extends BasePayment {


    /**
     *    获取支付表单
     *
     *    @author    Garbin
     *    @param     array $order_info  待支付的订单信息，必须包含总费用及唯一外部交易号
     *    @return    array
     */
    function get_payform($order_info) {
        $order_info=$this->get_order_info($order_info);
        
        if(!$order_info){
            return false;
        }

        $alipay_config['private_key_path'] = 'key/rsa_private_key.pem';
        $alipay_config['ali_public_key_path'] = 'key/alipay_public_key.pem';
        $alipay_config['sign_type'] = strtoupper('MD5');
        $alipay_config['input_charset'] = strtolower('utf-8');
        $alipay_config['cacert'] = ROOT_PATH .'/includes/payments/epayalipaywap/lib' . '\\cacert.pem';
        $alipay_config['transport'] = 'http';
        $alipay_config['partner'] = Conf::get('payment_alipay_partner');
        $alipay_config['key'] = Conf::get('payment_alipay_key');

        require_once(ROOT_PATH ."/includes/payments/epayalipaywap/lib/alipay_submit.class.php");


        $format = "xml";
        $v = "2.0";
        $req_id = date('Ymdhis');
        $notify_url = site_url(). "/notifyalipaywap.php";
        $call_back_url = site_url() . "/returnalipaywap.php";
        $merchant_url = site_url() . "/index.php?app=buyer_order";
        $seller_email = Conf::get('payment_alipay_seller_email');
        $out_trade_no = $order_info['order_sn'];
        $subject = $order_info['buyer_name'] . "充值" . $order_info['money'] . "元";
        $total_fee = $order_info['money'];
        $req_data = '<direct_trade_create_req><notify_url>' . $notify_url . '</notify_url><call_back_url>' . $call_back_url . '</call_back_url><seller_account_name>' . $seller_email . '</seller_account_name><out_trade_no>' . $out_trade_no . '</out_trade_no><subject>' . $subject . '</subject><total_fee>' . $total_fee . '</total_fee><merchant_url>' . $merchant_url . '</merchant_url></direct_trade_create_req>';
        $para_token = array(
            "service" => "alipay.wap.trade.create.direct",
            "partner" => trim($alipay_config['partner']),
            "sec_id" => trim($alipay_config['sign_type']),
            "format" => $format,
            "v" => $v,
            "req_id" => $req_id,
            "req_data" => $req_data,
            "_input_charset" => trim(strtolower($alipay_config['input_charset']))
        );
        $alipaySubmit = new AlipaySubmit($alipay_config);
        $html_text = $alipaySubmit->buildRequestHttp($para_token);
        $html_text = urldecode($html_text);
        $para_html_text = $alipaySubmit->parseResponse($html_text);
        $request_token = $para_html_text['request_token'];

        $req_data = '<auth_and_execute_req><request_token>' . $request_token . '</request_token></auth_and_execute_req>';
        $parameter = array(
            "service" => "alipay.wap.auth.authAndExecute",
            "partner" => trim($alipay_config['partner']),
            "sec_id" => trim($alipay_config['sign_type']),
            "format" => $format,
            "v" => $v,
            "req_id" => $req_id,
            "req_data" => $req_data,
            "_input_charset" => trim(strtolower($alipay_config['input_charset']))
        );
        $alipaySubmit = new AlipaySubmit($alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter, 'get', '确认');
        header("Content-type: text/html; charset=utf-8");
        echo $html_text;
        exit;
    }

    function _create_return_url($order_id) {
        return site_url() . "/index.php?app=buyer_order";
    }

    /**
     *    返回通知结果
     *
     *    @author    Garbin
     *    @param     array $order_info
     *    @param     bool  $strict
     *    @return    array
     */
    function verify_notify() {
        $alipay_config['private_key_path'] = ROOT_PATH .'/includes/payments/epayalipaywap/key/rsa_private_key.pem';
        $alipay_config['ali_public_key_path'] = ROOT_PATH .'/includes/payments/epayalipaywap/key/alipay_public_key.pem';
        $alipay_config['sign_type'] = strtoupper('MD5');
        $alipay_config['input_charset'] = strtolower('utf-8');
        $alipay_config['cacert'] = ROOT_PATH .'/includes/payments/epayalipaywap/lib' . '\\cacert.pem';
        $alipay_config['transport'] = 'http';
        $alipay_config['partner'] = Conf::get('payment_alipay_partner');
        $alipay_config['key'] = Conf::get('payment_alipay_key');

        require_once(ROOT_PATH ."/includes/payments/epayalipaywap/lib/alipay_notify.class.php");

        $alipayNotify = new AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyNotify();

        if ($verify_result) {//验证成功
            $doc = new DOMDocument();
            if ($alipay_config['sign_type'] == 'MD5') {
                $doc->loadXML($_POST['notify_data']);
            }

            if ($alipay_config['sign_type'] == '0001') {
                $doc->loadXML($alipayNotify->decrypt($_POST['notify_data']));
            }


            if (!empty($doc->getElementsByTagName("notify")->item(0)->nodeValue)) {

                $out_trade_no = $doc->getElementsByTagName("out_trade_no")->item(0)->nodeValue;
                //支付宝交易号
                $trade_no = $doc->getElementsByTagName("trade_no")->item(0)->nodeValue;
                //交易状态
                $trade_status = $doc->getElementsByTagName("trade_status")->item(0)->nodeValue;

                $total_fee = $doc->getElementsByTagName("total_fee")->item(0)->nodeValue;

                if ($trade_status == 'TRADE_FINISHED') {
                    $notify_result = array(
                        'trade_status' => '0',
                    );
                } else if ($trade_status == 'TRADE_SUCCESS') {
                    $notify_result = array(
                        'out_trade_no' => $out_trade_no, #商户订单号
                        'trade_no' => $trade_no, #交易凭据单号
                        'total_fee' => $total_fee, #涉及金额
                        'trade_status' => '1',
                    );
                }
            }
        } else {
            $notify_result = array(
                'trade_status' => '0',
            );
        }
        return $notify_result;
    }

    function verify_return() {
        $alipay_config['private_key_path'] = ROOT_PATH .'/includes/payments/epayalipaywap/key/rsa_private_key.pem';
        $alipay_config['ali_public_key_path'] = ROOT_PATH .'/includes/payments/epayalipaywap/key/alipay_public_key.pem';
        $alipay_config['sign_type'] = strtoupper('MD5');
        $alipay_config['input_charset'] = strtolower('utf-8');
        $alipay_config['cacert'] = ROOT_PATH .'/includes/payments/epayalipaywap/lib' . '\\cacert.pem';
        $alipay_config['transport'] = 'http';
        $alipay_config['partner'] = Conf::get('payment_alipay_partner');
        $alipay_config['key'] = Conf::get('payment_alipay_key');

        require_once(ROOT_PATH ."/includes/payments/epayalipaywap/lib/alipay_notify.class.php");

        $alipayNotify = new AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyReturn();

        if ($verify_result) {
            $out_trade_no = $_GET['out_trade_no'];//商户订单号
            $trade_no = $_GET['trade_no'];//支付宝交易号
            $result = $_GET['result'];//交易状态
            
            //判断是否为订单 为订单则跳转到订单页面
            $order_mod = &m('order');
            $order = $order_mod->get('order_sn='.$out_trade_no);
            if($order){
                header('Location: index.php?app=buyer_order&act=view&order_id='.$order['order_id']);
            }else{
                header('Location: index.php?app=account_log&act=logall');
            }
        } else {
            header('Location: index.php?app=member');
        }
    }

}

?>